<template>
  <div>
    <el-upload
        class="upload-demo"
        action="  <!--你自己的阿里云存储的地址-->    "
        ref="upload"
        :auto-upload="false"
        :data="this.objectData"
        :on-change="getPolity"
        :file-list="fileList">
      <el-button style="margin-left: 10px;" size="small" type="success" >上传到服务器</el-button>

      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
    <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      objectData:{
        OSSAccessKeyId:'',
        dir:'',
        expire:'',
        key:'',
        host:'',
        policy:'',
        signature:''
      },
      fileList: [{
        name: 'food.jpeg',
        url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }, {
        name: 'food2.jpeg',
        url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }]
    };
  },
  methods: {
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    submitUpload() {

      this.$refs.upload.submit();
      console.log(111)
      console.log(this.objectData)
    },
    getPolity(){
      const _this=this
      _this.$axios.get('http://localhost:8080/getOss').then(function (resp) {
        // if(resp.data===null){
        //   return false
        // }
        // else{
        console.log(resp.data)
        _this.objectData.key = resp.data.dir +'_${filename}';
        _this.objectData.OSSAccessKeyId=resp.data.accessId
        _this.objectData.dir=resp.data.dir
        _this.objectData.expire=resp.data.expire
        _this.objectData.host=resp.data.host
        _this.objectData.policy=resp.data.policy
        _this.objectData.signature=resp.data.signature
        console.log(_this.objectData)
      })
    }
  }
}
</script>